package top.as.sean.api.controller.system;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.as.sean.biz.system.IUser;
import top.as.sean.common.response.Result;
import top.as.sean.dao.dto.system.AuthUserDto;
import top.as.sean.dao.dto.system.DeleteDto;
import top.as.sean.dao.dto.system.LoginDto;
import top.as.sean.dao.dto.system.UserAdd;
import top.as.sean.dao.dto.system.UserEdit;
import top.as.sean.dao.dto.system.UserPage;
import top.as.sean.dao.vo.system.AuthUserVo;
import top.as.sean.dao.vo.system.MenuVo;
import top.as.sean.dao.vo.system.UserInfo;
import top.as.sean.dao.vo.system.UserPageVo;

import java.util.List;

/**
 * 用户接口
 *
 * @author: ArchieSean
 * @create: 2022/11/5
 */
@RestController
@RequestMapping("user")
@AllArgsConstructor
public class UserController {
    /**
     * 用户
     */
    private final IUser iUser;

    /**
     * 分页查询用户信息
     *
     * @param dto 入参
     * @return result
     */
    @SaCheckPermission("user:page")
    @GetMapping("page")
    public Result<Page<UserPageVo>> userPage(@Validated @ModelAttribute UserPage dto) {
        return Result.success(iUser.userPage(dto));
    }

    /**
     * 新增用户信息
     *
     * @return boolean
     */
    @SaCheckPermission("user:add")
    @PostMapping("add")
    public Result<Boolean> addUser(@Validated @RequestBody UserAdd dto) {
        return Result.success(iUser.addUser(dto));
    }

    /**
     * 编辑用户
     *
     * @param dto 入参
     * @return boolean
     */
    @SaCheckPermission("user:edit")
    @PutMapping("edit")
    public Result<Boolean> editUser(@Validated @RequestBody UserEdit dto) {
        return Result.success(iUser.editUser(dto));
    }

    /**
     * 登录接口
     *
     * @param dto 入参
     * @return userfinfo 包含token、用户相关信息
     */
    @PostMapping("login")
    public Result<String> doLogin(@Validated @RequestBody LoginDto dto) {
        return Result.success(iUser.login(dto));
    }

    /**
     * 获取用户信息
     *
     * @return userinfo
     */
    @SaCheckLogin
    @GetMapping("info")
    public Result<UserInfo> getInfo() {
        return Result.success(iUser.getInfo());
    }

    /**
     * 获取动态路由
     *
     * @return list
     */
    @SaCheckLogin
    @GetMapping("router")
    public Result<List<MenuVo>> getRouter() {
        return Result.success(iUser.getRouter());
    }

    /**
     * 退出
     * @return obj
     */
    @SaCheckLogin
    @PostMapping("out")
    public Result<?> logout() {
        iUser.lagoutUser();
        return Result.success();
    }

    /**
     * 删除用户
     *
     * @param dto 入参
     * @return boolean
     */
    @PostMapping("del")
    public Result<?> deleter(@Validated @RequestBody DeleteDto dto) {
        iUser.deleteUser(dto.getIds());
        return Result.success();
    }

    /**
     * 角色管理-查询用户列表
     *
     * @param dto 入参
     * @return page
     */
    @GetMapping("auth")
    public Result<Page<AuthUserVo>> getAuthList(@Validated @ModelAttribute AuthUserDto dto) {
        return Result.success(iUser.getAuthList(dto, false));
    }

    /**
     * 角色管理--添加用户列表
     *
     * @param dto 入参
     * @return page
     */
    @GetMapping("auth/list")
    public Result<Page<AuthUserVo>> getAuthAddPage(@Validated @ModelAttribute AuthUserDto dto) {
        return Result.success(iUser.getAuthList(dto, true));
    }
}
